package thinkinginjava.concurrency;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CountDemo {
	public static class Count {
		static Integer c = 0;
		Integer p;
		
		private synchronized static Integer count() {
			return ++c;
		}
		
		public void getNumber() {
			p = count();
		}
	}
	
	public static void main(String[] args) {
		final ConcurrentHashMap<Integer, Integer> set = new ConcurrentHashMap<Integer, Integer>();
		for(int i = 0; i < 100000; i ++) {
			Thread t = new Thread(new Runnable() {

				@Override
				public void run() {
					Count c = new Count();
					c.getNumber();
//					System.out.println(Thread.currentThread().getId() + " -" + c.p);
//					
					System.out.println(c.p);
					if(set.putIfAbsent(c.p, c.p) != null) {
						System.out.println("duplicated");;
					}
//					System.out.println(set.size());
				}
				
			});	
			
			t.start();
		}
	
		
	}
}
